// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init();

// 云函数入口函数
exports.main = async (event, context) => {
    const wxContext = cloud.getWXContext()
    const db = cloud.database();
    const _ = db.command;
    const NWPU = db.collection("NWPU");
    const QUEUE = db.collection("NWPU-Queue");
    const ORDER = db.collection("orderHistory");
    const domNames = ['星A', '星B', '星C', '星D', '星E', '星F', '星G', '星H',
        '云A', '云B', '云C', '云D', '云E', '云F', '云G', '云H'];
    
    for (let domName of domNames) {
        QUEUE.doc(domName).get().then(queueOfDom => {
            NWPU.doc(domName).get().then(dom => {
                for (let floor of [1, 2, 3, 4, 5, 6]) {
                    let queue = queueOfDom.data[floor];
                    let status = dom.data[floor];
                    for (let i = 0; i < status.length; ++i) {
                        if (status[i]) {
                            let orderID = queue.shift();
                            if (!orderID) {
                                break;
                            }
                            status[i] = false;
                            ORDER.doc(orderID).get().then(res => {
                                console.log("++++++++++++++++++++++++")
                                console.log(res.data._openid, orderID);
                                console.log("++++++++++++++++++++++++++");
                                cloud.callFunction({
                                    name: "sendShowerMessage",
                                    data: {
                                        openid: res.data._openid,
                                        orderID: orderID,
                                        roomID: domName + " " + floor + "层" + (i + 1) + "号"
                                        // roomID: "嘻嘻嘻"
                                    }
                                }).then(suces => {
                                    console.log("success send message to ", res.data._openid, "\n", suces);
                                }).catch(err => {
                                    console.error("fail send message to ", res.data._openid, "\n", err);
                                });
                            }).catch(err => {
                                console.error(err);
                            });
                        }
                    }
                }
                let newdata = {};
                for (let key in dom.data) {
                    if (key == "_id" || key == "_openid") { continue; }
                    newdata[key] = dom.data[key];
                }
                NWPU.doc(domName).update({
                    data: newdata
                }).then(res => {
                    console.log("success update que", domName);
                }).catch(err => {
                    console.error(err);
                });
                newdata = {};
                for (let key in queueOfDom.data) {
                    if (key == "_id" || key == "_openid") { continue; }
                    newdata[key] = queueOfDom.data[key];
                }
                QUEUE.doc(domName).update({
                    data: newdata
                }).then(res => {
                    console.log("success update dom", domName);
                }).catch(err => {
                    console.error(err);
                })
            }).catch(err => {
                console.error(err);
            });
        }).catch(err => {
            console.error(err);
        });
        break;
    }

}
